const {
	Service
} = require('uni-cloud-router');
const {
	getStockDWMKLine
} = require("lg-request-stock");
const {
	parser,
	success,
	exception,
	error,
	typeOf,
	Clock,
	timingLogger,
	StatusEnum
} = require("lg-utils");
const db = uniCloud.database();
const dbCmd = db.command;
module.exports = class CommonService extends Service {
	async xxx() {
		try {
			return "xxxxxxx"
		} catch (e) {
			throw e
		}

	}
	async getMarketIsCloseWithA() {
		try {
			const {
				success:succ,
				data,
				errorMessage
			} = await this.getMarketsInfoList() || {};
			if (succ) {
				const {
					SZ: {
						statusName: szStatusName
					} = {},
					SH: {
						statusName: shStatusName
					} = {}
				} = data;
				return success(szStatusName == "已收盘" && shStatusName == "已收盘");
			} else {
				return error(errorMessage);
			}
		} catch (e) {
			throw e
		}

	}
	async getMarketsInfoList({
		symbol = "sz000100",
		cycle = 0,
		type = StatusEnum.DAY
	} = {}) {
		try {
			const {
				status,
				data
			} = await getStockDWMKLine({
				symbol,
				cycle,
				type
			}) || {};

			if (StatusEnum.SUCCESS == status) {
				const {
					data: {
						[symbol]: {
							qt: {
								market: {
									0: marketInfo
								} = {}
							} = {}
						} = []
					} = {}
				} = data || {};
				const marketInfoModel = {};
				const marketInfList = marketInfo.split("|");
				const currDateTime = marketInfList.shift();
				marketInfoModel.curDateTime = currDateTime;
				for (let infoItem of marketInfList) {
					const infoItemList = infoItem.split("_");
					// SZ 深圳 SH 上海
					marketInfoModel[infoItemList[0]] = {
						status: infoItemList[1], // close open
						statusName: infoItemList[2], // 已收盘 未开盘 交易中 盘前交易 已休市
					}
				}

				return success(marketInfoModel);
			} else {
				return error("远程K线请求异常;", status);
			}

		} catch (e) {
			throw e
		}

	}
}
